package com.zhuxin.medical.mvc.controller;

import com.CommWxApplication;
import com.dbHelper.DbTemplate;
import com.google.gson.Gson;
import com.zhuxin.common.BussResp;
import com.zhuxin.common.UtilTools;
import com.zhuxin.medical.model.po.CoRuPlan;
import com.zhuxin.medical.model.po.CommAppLog;
import com.zhuxin.medical.model.po.CommWxUser;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@RequestMapping("/wxPage")
public class WxPageCtr {


    @Autowired
    private DbTemplate mysqlDao;

    @Autowired
    private DbTemplate oracleDao;

    @Value("${wx.domainName.test}")
    private String domainName;//域名

    @RequestMapping("/WxCancelPlanInit")
    public String WxCancelPlanInit(HttpServletRequest request, HttpServletResponse response, Model model){
        CommAppLog log=new CommAppLog();
        log.setRecid(request.getParameter("infoId"));
        log=this.mysqlDao.queryVoByKey(log);
        Map<String,String> plans=new Gson().fromJson(log.getPlaninf(),Map.class);
        model.addAttribute("info",plans);
        model.addAttribute("log",log);
        model.addAttribute("infoId",request.getParameter("infoId"));
        return "/canelinfo/CanelInfo";
    }

    @RequestMapping("/WxEnablePlan")
    public String WxEnablePlan(HttpServletRequest request, HttpServletResponse response, Model model){
        List<CommWxUser> user = this.mysqlDao.querySub("select * from commWxUser where openid=? and usertype=0", CommWxApplication.threadMap.get().get("openid"), CommWxUser.class);
        if(user.isEmpty()){
            model.addAttribute("msg","你没有执行此操作的权限");
            return "/canelinfo/msg";
        }
        String infoId = request.getParameter("infoId");
        CommAppLog log=new CommAppLog();
        log.setRecid(infoId);
        log=this.mysqlDao.queryVoByKey(log);
        if(log.getStatus().intValue()==3 && log.getOperid().equals(CommWxApplication.threadMap.get().get("openid"))) {
            String[] yjplanguids = log.getPlanids().split(",");
            for (String yjplanguid : yjplanguids) {
                CoRuPlan pkVO = new CoRuPlan();
                pkVO.setYjplanid(Long.valueOf(yjplanguid));
                pkVO.setStatus(2L);
                this.oracleDao.updateSub(pkVO, "status", true);
            }
            log.setStatus(4L);
            log.setEnabletime(new Date());
//            log.setApproveid(user.get(0).getOpenid());
//            log.setApprover(user.get(0).getUsercode());
//            log.setApprovetime(new Date());
            this.mysqlDao.updateSub(log, ArrayUtils.toArray("status", "enabletime"), true);
//            user.get(0).setStatus(0);
//            this.mysqlDao.updateSub(user.get(0), "status", true);
            UtilTools.sendKfMsg(log.getOperid(),"方案:"+log.getPlanids()+"已全部生效.");
            model.addAttribute("msg", "执行完成.");
        }else{
            model.addAttribute("msg", "你没有执行此操作的权限或已被处理.");
        }
        return "/canelinfo/msg";
    }

    @RequestMapping("/WxCancelPlanWithoutEnable")
    public String WxCancelPlanWithoutEnable(HttpServletRequest request, HttpServletResponse response, Model model){
        //权限校验
        List<CommWxUser> user = this.mysqlDao.querySub("select * from commWxUser where openid=? and usertype=1", CommWxApplication.threadMap.get().get("openid"), CommWxUser.class);
        if(user.isEmpty()){
            model.addAttribute("msg","你没有执行此操作的权限");
            return "/canelinfo/msg";
        }
        String infoId = request.getParameter("infoId");
        CommAppLog log=new CommAppLog();
        log.setRecid(infoId);
        log=this.mysqlDao.queryVoByKey(log);
        if(log.getStatus().intValue()==2) {
            String[] yjplanguids = log.getPlanids().split(",");
            for (String yjplanguid : yjplanguids) {
                CoRuPlan pkVO = new CoRuPlan();
                pkVO.setYjplanid(Long.valueOf(yjplanguid));
                pkVO.setStatus(-1L);
                this.oracleDao.updateSub(pkVO, "status", true);
            }
            log.setStatus(5L);
            log.setApproveid(user.get(0).getOpenid());
            log.setApprover(user.get(0).getUsercode());
            log.setApprovetime(new Date());
            this.mysqlDao.updateSub(log, ArrayUtils.toArray("status", "Approveid", "approver", "Approvetime"), true);
            user.get(0).setStatus(0);
            user.get(0).setOpenid(log.getOperid());
            this.mysqlDao.updateSub(user.get(0), "status", true);
            StringBuffer sb=new StringBuffer("您申请作废的方案");
            sb.append("您申请作废的方案").append(log.getPlanids()).append("已被通过，方案已经成功作废。请修改完成后点击")
                    .append("<a href='").append("http://"+domainName+"/wxPage/WxCancelPlanInit?infoId="+log.getRecid()).append("'>立即生效").append("</a> ").append("对方案进行生效.(未修改完成请勿点击链接！)");
            UtilTools.sendKfMsg(log.getOperid(),sb.toString());
            model.addAttribute("msg", "执行完成.");
        }else{
            model.addAttribute("msg", "已被处理，无需重复执行!");
        }
        return "/canelinfo/msg";
    }

    @RequestMapping("/WxCancelPlan")
    public String WxCancelPlan(HttpServletRequest request, HttpServletResponse response, Model model){
        //权限校验
        List<CommWxUser> user = this.mysqlDao.querySub("select * from commWxUser where openid=? and usertype=1", CommWxApplication.threadMap.get().get("openid"), CommWxUser.class);
        if(user.isEmpty()){
            model.addAttribute("msg","你没有执行此操作的权限");
            return "/canelinfo/msg";
        }
        String infoId = request.getParameter("infoId");
        CommAppLog log=new CommAppLog();
        log.setRecid(infoId);
        log=this.mysqlDao.queryVoByKey(log);
        if(log.getStatus().intValue()==2) {
            String[] yjplanguids = log.getPlanids().split(",");
            for (String yjplanguid : yjplanguids) {
                CoRuPlan pkVO = new CoRuPlan();
                pkVO.setYjplanid(Long.valueOf(yjplanguid));
                pkVO.setStatus(-1L);
                this.oracleDao.updateSub(pkVO, "status", true);
            }
            log.setStatus(3L);
            log.setApproveid(user.get(0).getOpenid());
            log.setApprover(user.get(0).getUsercode());
            log.setApprovetime(new Date());
            this.mysqlDao.updateSub(log, ArrayUtils.toArray("status", "Approveid", "approver", "Approvetime"), true);
            user.get(0).setStatus(0);
            user.get(0).setOpenid(log.getOperid());
            this.mysqlDao.updateSub(user.get(0), "status", true);
            StringBuffer sb=new StringBuffer("您申请作废的方案");
            sb.append("您申请作废的方案").append(log.getPlanids()).append("已被通过，方案已经成功作废。请修改完成后点击")
                    .append("<a href='").append("http://"+domainName+"/wxPage/WxCancelPlanInit?infoId="+log.getRecid()).append("'>立即生效").append("</a> ").append("对方案进行生效.(未修改完成请勿点击链接！)");
            UtilTools.sendKfMsg(log.getOperid(),sb.toString());
            model.addAttribute("msg", "执行完成.");
        }else{
            model.addAttribute("msg", "已被处理，无需重复执行!");
        }
        return "/canelinfo/msg";
    }
}
